﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.Domain.Interfaces;
using WebNext.UI.Models.Login;
using WebNext.UI.Properties;
using WebNext.Domain;
using WebNext.Domain.Entities;
using WebNext.UI.Utils;
using WebNext.UI.Models;
using System.Web.Security;
using WebNext.UI.Utils.Mailing;

namespace WebNext.UI.Controllers
{
	public class LoginController : BaseController
	{
		private IAdminSettingsRepository adminRepository;

		public LoginController(IAdminSettingsRepository adminRepository)
		{
			this.adminRepository = adminRepository;
		}

		public ActionResult Login()
		{
			LoginViewModel loginViewModel = new LoginViewModel(string.Empty, string.Empty);
			return View(loginViewModel);
		}

		[HttpPost]
		public ActionResult Login(LoginViewModel loginModel)
		{
			ValidationResult result = loginModel.Validate();
			if (result.IsValid)
			{
				AdminSettings adminSettings = this.adminRepository.GetSettings();
				PasswordHelper passwordHelper = new PasswordHelper();
				string passwordHash = passwordHelper.Hash(loginModel.Password);
				if (loginModel.UserName == adminSettings.UserName && passwordHash == adminSettings.Password)
				{
					FormsAuthentication.SetAuthCookie(loginModel.UserName, false);
					this.TempData[Constants.Message] = Properties.Resources.LoginSuccessfull;
					return this.RedirectToAction("List", "Entries", new { viewType = MainViewType.Blog, pageNumber = 1 });
				}
				this.TempData[Constants.Message] = Properties.Resources.UserNameOrPasswordInvalid;
			}
			else
			{
				this.TempData[Constants.Message] = result.GetErrorsMessage();
			}
			return this.View(loginModel);
		}

		[HttpPost]
		[Authorize]
		public ActionResult LogOff()
		{
			FormsAuthentication.SignOut();
			this.TempData[Constants.Message] = Resources.LogoffSuccessfull;
			return this.RedirectToAction("List", "Entries", new { viewType = MainViewType.Blog, pageNumber = 1 });
		}

		public ActionResult ResetPassword()
		{
			return this.View(new ResetPasswordViewModel(string.Empty));
		}

		[HttpPost]
		public ActionResult ResetPassword(ResetPasswordViewModel resetPasswordModel)
		{
			ValidationResult result = resetPasswordModel.Validate();
			if (result.IsValid)
			{
				AdminSettings adminSettings = this.adminRepository.GetSettings();
				if (resetPasswordModel.Email == adminSettings.Email)
				{
					PasswordHelper passwordHelper = new PasswordHelper();
					string newPassword = passwordHelper.Generate();
					adminSettings.Password = passwordHelper.Hash(newPassword);

					IEmailNotifier emailNotifier = new GmailNotifier();
					emailNotifier.Notify(adminSettings.Email, Properties.Resources.NewPasswordNotificationSubject, string.Format(Properties.Resources.NewPasswordNotificationBody, newPassword));

					this.TempData[Constants.Message] = Properties.Resources.NewPasswordHasBeenSent;
					return this.RedirectToAction("List", "Entries", new { viewType = MainViewType.Blog, pageNumber = 1 });
				}
				this.TempData[Constants.Message] = Properties.Resources.EmailAddressIsInvalid;
				resetPasswordModel.IsEmailValid = false;
			}
			else
			{
				this.TempData[Constants.Message] = result.GetErrorsMessage();
			}
			return this.View(resetPasswordModel);
		}
	}
}
